﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Security.Principal;
using pwblog.BusinessObject;
using pwblog.Entities;

namespace pwblog.Web
{
    public partial class Site : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadDataByCotegory();

            // neu la mot creator thi hien menu danh cho creator len
            if (HttpContext.Current.User.IsInRole("creator"))
            {
                menuCreator.Visible = true;
            }

            // neu la mot admin thi hien menu danh cho admin len
            if (HttpContext.Current.User.IsInRole("admin"))
            {
                menuCreator.Visible = true;
                menuAdmin.Visible = true;
            }
        }

        private void LoadDataByCotegory()
        {
            Category[] categories = new CategoryBo().GetCategories();
            if(categories != null)
            {
                foreach (Category category in categories)
                {
                    menuCategory.Items.Add(new MenuItem() { 
                        Text = category.Name,
                        Value = category.Id.ToString()
                    });
                }
            }
        }

        protected void MenuCategory_MenuItemClick(object sender, MenuEventArgs e)
        {
            string idCategory = menuCategory.SelectedValue;
            Response.Redirect(@"\Web\ArticlesCategory.aspx?idCategory=" + idCategory);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string key = tbSearch.Text;
            Response.Redirect(@"\Web\SearchArticle.aspx?key=" + key);
        }

        public string ConvertRelativeUrlToAbsoluteUrl(string relativeUrl)
        {

            if (Request.IsSecureConnection)

                return string.Format("https://{0}:{1}{2}", Request.Url.Host, Request.Url.Port, Page.ResolveUrl(relativeUrl));

            else

                return string.Format("http://{0}:{1}{2}", Request.Url.Host, Request.Url.Port, Page.ResolveUrl(relativeUrl));

        }
    }
}